package  com.stars.home.update.util;

import android.os.Environment;
import android.os.StatFs;


import com.stars.home.App;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.List;



public class FileUtil {

    /**
     * 获取外部缓存路径
     *
     * @return
     */
    public static File getExternalCacheDir() {
        return App.getApp().getExternalCacheDir();
    }

    /**
     * 获取内部缓存路径
     *
     * @return
     */
    public static File getCacheDir() {
        if (App.getApp() == null) {
            return null;
        }
        return App.getApp().getCacheDir();
    }


    /**
     * 获取内部文件路径
     *
     * @return
     */
    public static File getFileDir() {
        return App.getApp().getFilesDir();
    }

    /**
     * 获取下载目录
     *
     * @return
     */
    public static File getDownloadDir() {
        return App.getApp().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
    }

    /**
     * 获取图片目录
     *
     * @return
     */
    public static File getPicDir() {
        return App.getApp().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    }

    /**
     * 判断该目录是否为空
     */
    public static Boolean isFileEmpty(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        if (!file.isFile() && file.list() != null && file.listFiles().length > 0) {
//            for (File mfile: file.listFiles()){
//                Log.d("luo",mfile.getName());
//            }
            return false;
        }
        return true;
    }

    /**
     * 判断是否存在该文件
     */
    public static Boolean isFileExists(String filePath){
        return new File(filePath).exists();
    }

    /**
     * 重新文件目录并返回绝对路径
     *
     * @return
     */
    public static String resetFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }

    /**
     * 重新文件目录并返回绝对路径
     *
     * @return
     */
    public static void delectAnyFile(String filePath, List<String> fileNames) {
        if (fileNames == null || fileNames.size() == 0){
            return;
        }

        File file = new File(filePath);
        if (!file.exists()) {
            return;
        }

        if (file.isFile() || file.list() == null || file.listFiles().length == 0) {
            return;
        }

        for (File mfile : file.listFiles()) {
            for (String name : fileNames) {
                if (!mfile.getName().equals(name)) {
                    mfile.delete();
                }
            }
        }
    }

    public static byte[] getFileByte(File file) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
        return buffer;
    }


    public static String getString(InputStream inputStream) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int len = -1;
        byte[] buffer = new byte[1024];
        try {
            while ((len = inputStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            return baos.toString();
        } catch (Exception e) {
        }
        return "";
    }

    /**
     * 删除指定路经文件
     *
     * @param path 文件路经
     */
    public static boolean delFile(String path) {
        File file = new File(path);
        boolean del = false;
        if (file.exists()) {
            del = file.delete();
        }
        return del;
    }


    public static long getSDCardAvailSize() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());
            long blockSize = sf.getBlockSizeLong();
            long availCount = sf.getAvailableBlocksLong();
            return availCount * blockSize;
        }
        return 0L;
    }


    /**
     * 把数据写入到文件中
     *
     * @param save
     * @param file
     */
    public static void save(String save, File file) {
        try {
            FileOutputStream fos = new FileOutputStream(file);
            Writer out = new OutputStreamWriter(fos, "UTF-8");
            out.write(save);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param file
     * @return
     */
    public static String readFile(File file) {
        if (file == null || !file.exists()) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        try {
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            Reader in = new BufferedReader(isr);
            int i;
            while ((i = in.read()) > -1) {
                buffer.append((char) i);
            }
            in.close();
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
